package com.quotes.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.apache.log4j.Logger;

import com.quotes.datamodell.*;

public class SymbolsPanel extends JPanel implements ActionListener, DataChangedEventListener
{
	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(getClass().toString());
	private DataRepository repository = null;
	
	public SymbolsPanel(DataRepository repository) {
		this.repository = repository;
		this.repository.addDataChangedEventListener(this);
		
		this.setLayout(new BorderLayout());
		SymbolsTable symbolsTable = new SymbolsTable(repository);
		symbolsTable.setPreferredSize(new Dimension(300, 200));
		JPanel mainPanel = new JPanel();
		JSplitPane oneSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, symbolsTable, mainPanel);
		this.add(oneSplitPane, BorderLayout.CENTER);
		
		JButton dbLoadButton = new JButton("Load Quotes from DB");
		dbLoadButton.setActionCommand("loadFromDB");
		dbLoadButton.addActionListener(this);
		mainPanel.add(dbLoadButton);
		
		JButton dbSaveButton = new JButton("Save Quotes to DB");
		dbSaveButton.setActionCommand("saveToDB");
		dbSaveButton.addActionListener(this);
		mainPanel.add(dbSaveButton);
		
		JButton excelLoadButton = new JButton("Load Quotes from Excel");
		excelLoadButton.setActionCommand("loadFromExcel");
		excelLoadButton.addActionListener(this);
		mainPanel.add(excelLoadButton);
		
		JButton excelSaveButton = new JButton("Save Quotes to Excel");
		excelSaveButton.setActionCommand("saveToExcel");
		excelSaveButton.addActionListener(this);
		excelSaveButton.setEnabled(false);
		mainPanel.add(excelSaveButton);
		
		JButton yahooLoadButton = new JButton("Load Quotes from Yahoo");
		yahooLoadButton.setActionCommand("loadFromYahoo");
		yahooLoadButton.addActionListener(this);
		mainPanel.add(yahooLoadButton);
		
		logger.debug("SymbolsPanel created!");
	}
	
	public DataRepository getRepository() {
		return repository;
	}
	
	public void setEnabledAllButtons(boolean enabled) {
		for (Component c : this.getComponents()) {
			if (c instanceof JButton) {
				JButton button = (JButton)c;
				button.setEnabled(enabled);
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		logger.debug("Action invoked: " + event.getActionCommand());
		
		if (event.getActionCommand() == "loadFromExcel") {
			Thread thread = new Thread(new Runnable() {
				public void run() {
					setEnabledAllButtons(false);
					repository.tryLoadFromExcel();
					setEnabledAllButtons(true);
				}
			});
			thread.start();
		} else if (event.getActionCommand() == "loadFromYahoo") {
			Thread thread = new Thread(new Runnable() {
				public void run() {
					setEnabledAllButtons(false);
					repository.tryLoadFromYahoo();
					setEnabledAllButtons(true);
				}
			});
			thread.start();
		} else if (event.getActionCommand() == "saveToExcel") {
			Thread thread = new Thread(new Runnable() {
				public void run() {
					setEnabledAllButtons(false);
					repository.trySaveToExcel();
					setEnabledAllButtons(true);
				}
			});
			thread.start();			
		} else if (event.getActionCommand() == "loadFromDB") {
			Thread thread = new Thread(new Runnable() {
				public void run() {
					setEnabledAllButtons(false);
					repository.tryLoadFromDb();
					setEnabledAllButtons(true);
				}
			});
			thread.start();			
		} else if (event.getActionCommand() == "saveToDB") {
			Thread thread = new Thread(new Runnable() {
				public void run() {
					setEnabledAllButtons(false);
					repository.trySaveToDb();
					setEnabledAllButtons(true);
				}
			});
			thread.start();			
		}
	}

	@Override
	public void dataChanged(DataRepository repository) {
		//this.excelSaveButton.setEnabled(repository.isDataLoaded());
	}
}
